package com.example.myyunpan.Config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private CorsProperties corsProperties;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 允许所有路径
                .allowedOrigins(corsProperties.getAllowedOrigins().split(",")) // 允许的源
                .allowedMethods(corsProperties.getAllowedMethods()) // 允许的方法
                .allowedHeaders(corsProperties.getAllowedHeaders()) // 允许的头
                .allowCredentials(corsProperties.isAllowCredentials()) // 是否允许发送Cookie
                .maxAge(corsProperties.getMaxAge()); // 预检请求的有效期，单位为秒
    }

}
